#!/bin/bash
#本脚本在于将环境配置好，让用户可以只用配置清单文件，便可启动剧本
read -p "请输入第一台控制主机的ip："  host01
read -p "请输入第二台控制主机的ip："  host02

#安装git，从远程拉取代码
yum -y install git &>/dev/null
mkdir /gitee
cd /gitee
echo "开始拉取仓库代码"
git clone https://gitee.com/xiaojie3125/redis_cluster.git 
ls /gitee/redis_cluster
if [ $? -ne 0  ];then
   echo"代码拉取失败"
   exit 1
fi
#将仓库中的代码拷贝到目的目录
cp -r /gitee/redis_cluster/redis /etc/ansible/roles/
mkdir /bash
cp   /gitee//redis_cluster/redis_start.sh /bash
chmod a+x /bash/redis_start.sh
#替换判断条件中的hosts
sed -ri "s|192.168.2.20|$host01|g" /etc/ansible/roles/redis/tasks/main.yml
sed -ri "s|192.168.2.30|$host02|g" /etc/ansible/roles/redis/tasks/main.yml
grep $host01 /etc/ansible/roles/redis/tasks/main.yml &>/dev/null

if [ $? -eq 0  ];then
   echo "/tasks/main.yml替换成功"
else
   echo "替换失败"
   exit 2
fi

sed -ri "s|192.168.2.20|$host01|g" /bash/redis_start.sh
sed -ri "s|192.168.2.30|$host02|g"  /bash/redis_start.sh

grep $host02 /bash/redis_start.sh &>/dev/null

if [ $? -eq 0  ];then
   echo "脚本替换成功"
else
   echo "脚本替换失败"
   exit 2
fi

echo "环境替换完成，请配置清单文件和启动.yml"

